//
//  ConsultationRoomController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/3/9.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit
import JXSegmentedView
import FDFullscreenPopGesture

/// 就诊室 分页入口
class ConsultationRoomController: BaseViewController {
    
    public let segmented = JXSegmentedView()
    private let dataSource = JXSegmentedNumberDataSource()
    private var viewControllers: [JXSegmentedListContainerViewListDelegate] = []
    public var selectedIndex: Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        
        initLayout()
        initData()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: true)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        setNavClear()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        setNavWhite()
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    func initLayout() {
        let statusView = UIView()
        view.addSubview(statusView)
        statusView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(UIApplication.shared.statusBarFrame.size.height)
        }
        statusView.backgroundColor = .white
        
        view.addSubview(segmented)
        segmented.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(statusView.snp.bottom)
            make.height.equalTo(88)
        }
        segmented.backgroundColor = .white
        
        dataSource.titles = ["图文问诊", "护理咨询", "云门诊"]
        dataSource.numbers = [0, 0, 0]
        dataSource.itemWidth = screenW/3.0
        dataSource.titleNormalFont = .regular_18
        dataSource.titleSelectedFont = .medium(25)
        dataSource.titleNormalColor = .fontColor
        dataSource.titleSelectedColor = .mainColor
        dataSource.isItemSpacingAverageEnabled = false
        dataSource.itemSpacing = 0
        dataSource.numberBackgroundColor = .red
        segmented.dataSource = dataSource
        
        let indicator = JXSegmentedIndicatorLineView()
        segmented.indicators = [indicator]
        indicator.indicatorHeight = 8
        indicator.indicatorColor = .init(hex: "#F1F1F1")
        indicator.verticalOffset = 30
        
        let listContainer = JXSegmentedListContainerView(dataSource: self)
        view.addSubview(listContainer)
        listContainer.snp.makeConstraints { make in
            make.left.bottom.right.equalToSuperview()
            make.top.equalTo(segmented.snp.bottom)
        }
        segmented.listContainer = listContainer
    }
    
    func initData() {
        var indexOfData = 0 {
            didSet {
                let index = indexOfData - 1000
                segmented.selectItemAt(index: index)
            }
        }
        
        let advisoryVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "advisoryVcID") as! PMCAdvisoryViewController
        advisoryVc.status = .advisory
        advisoryVc.loadComplete = { [weak self] count in
            if count > 0 && indexOfData == 0 {
                indexOfData = 1000
            }
            self?.dataSource.numbers[0] = count
            self?.segmented.reloadDataWithoutListContainer()
        }
        advisoryVc.handleData(loadMore: false)
        
        let nurseVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "advisoryVcID") as! PMCAdvisoryViewController
        nurseVc.status = .nurse
        nurseVc.loadComplete = { [weak self] count in
            self?.dataSource.numbers[1] = count
            self?.segmented.reloadDataWithoutListContainer()
            if count > 0 && indexOfData == 0  {
                indexOfData = 1001
            }
        }
        nurseVc.handleData(loadMore: false)
        
        let cloudVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "cloudVcId") as! CloudViewController
        cloudVc.loadComplete = { [weak self] count in
            self?.dataSource.numbers[2] = count
            self?.segmented.reloadDataWithoutListContainer()
            if count > 0 && indexOfData == 0  {
                indexOfData = 1002
            }
        }
        cloudVc.handleData()

        viewControllers = [advisoryVc, nurseVc, cloudVc]
        segmented.reloadData()
    }
}

extension ConsultationRoomController: JXSegmentedListContainerViewDataSource {
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        return viewControllers[index]
    }
    
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return viewControllers.count
    }
}
